﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.LocalColor
{
    public partial class AddLocalColorNews :BasePage
    {
        NJL.BLL.NJL_LOCAL_COLOR_NEWS bll = new BLL.NJL_LOCAL_COLOR_NEWS();
        NJL.Model.NJL_LOCAL_COLOR_NEWS model = new Model.NJL_LOCAL_COLOR_NEWS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["submitstate"] = "1";
                bind();
            }
        }
        public void bind()
        {
            rptUserList.DataSource = bll.GetList("NJL_LOCAL_COLOR_ID='" + Request.QueryString["id"] + "'").Tables[0].DefaultView;
            rptUserList.DataBind();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["state"] != null)
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                model.NEWS_TITLE = txtTitle.Text.Trim();
                model.NEWS_SUBJECT_TITLE = txtSubtitle.Text.Trim();
                model.NEWS_TAG = txtTag.Text.Trim();
                model.NJL_LOCAL_COLOR_ID = Request.QueryString["id"];
                model.NEWS_CONTECT = txtContent.Text.Trim();
                model.SEO_TITLE = txtSeoTitle.Text.Trim();
                model.SEO_KEYWORDS = txtSeoKeyworkds.Text.Trim();
                model.SEO_DESCRIPTION = txtSeoDescription.Text.Trim();
                model.UPDATE_DATE_TIME = DateTime.Now;
                model.UPDATE_USER_NAME = USER_NAME;
                model.UPDATE_USER_ID = USER_ID;
                model.UPDATE_DATE_TIME = DateTime.Now;
                model.VISIT_COUNT = 0;
                model.COMMENT_COUNT = 0;
                model.SORT = int.Parse(txtSort.Text.Trim());
                if (FileUpload1.HasFile)
                {
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                }
                if (bll.Update(model))
                {
                   Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", Request.RawUrl);
                }
            }
            else
            {
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                model.NEWS_TITLE = txtTitle.Text.Trim();
                model.NEWS_SUBJECT_TITLE = txtSubtitle.Text.Trim();
                model.NEWS_TAG = txtTag.Text.Trim();
                model.NJL_LOCAL_COLOR_ID = Request.QueryString["id"];
                model.NEWS_CONTECT = txtContent.Text.Trim();
                model.SEO_TITLE = txtSeoTitle.Text.Trim();
                model.SEO_KEYWORDS = txtSeoKeyworkds.Text.Trim();
                model.SEO_DESCRIPTION = txtSeoDescription.Text.Trim();
                model.CREATE_DATE_TIME = DateTime.Now;
                model.CREATE_USER_NAME = USER_NAME;
                model.CREATE_USER_ID = USER_ID;
                model.VISIT_COUNT = 0;
                model.COMMENT_COUNT = 0;
                model.SORT = int.Parse(txtSort.Text.Trim());
                if (FileUpload1.HasFile)
                {
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                }
                if (bll.Add(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", Request.RawUrl);

                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal lit = e.Item.FindControl("litNum") as Literal;
            lit.Text = (e.Item.ItemIndex + 1).ToString();
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "Edit")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    Button1.Visible = true;
                    txtSort.Text = model.SORT.ToString();
                    txtTitle.Text = model.NEWS_TITLE;
                    txtTag.Text = model.NEWS_TAG;
                    txtContent.Text = model.NEWS_CONTECT;
                    txtSubtitle.Text = model.NEWS_SUBJECT_TITLE;
                    txtSeoTitle.Text = model.SEO_TITLE;
                    txtSeoKeyworkds.Text = model.SEO_KEYWORDS;
                    txtSeoDescription.Text = model.SEO_DESCRIPTION;
                    ViewState["state"] = "update";
                    ViewState["ID"] = id;
                    Reset1.Value = "返回";
                    Reset1.Attributes.Add("onclick", "location.href='" + Request.RawUrl + "'");
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                if (model != null)
                {

                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件

                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                    }
                }
            }
            bind();
        }

        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }


    }
}